package oa.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.alibaba.fastjson.JSON;
import com.mysql.fabric.xmlrpc.base.Array;

import oa.entity.Message;
import oa.entity.User;
import oa.paging.Page;
import oa.service.impl.DepartmentServiceImpl;
import oa.service.impl.UserServiceImpl;

/**
 * Servlet implementation class DeptUserInfo
 */
@WebServlet("/deptuserinfo")
public class DeptUserInfo extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public DeptUserInfo() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");
		response.setContentType("text/html;charset=utf-8");
		HttpSession session = request.getSession();
		PrintWriter out = response.getWriter();
		Map<String, Object> maplist = new HashMap<String, Object>(2);
		DepartmentServiceImpl depart = new DepartmentServiceImpl();
		UserServiceImpl user = new UserServiceImpl();
		List<User> userlist = new ArrayList<User>();
		User us = new User();
		String opt = request.getParameter("opt");
		String id = request.getParameter("companyidmoren");
		int companyid = (int)session.getAttribute("companyid");
		int companyidmoren = 0;
		if (id == null) {
			id = "0";
		}else if(id==null&&companyid==0) {
			companyidmoren =0;
		}else if(id!=null && companyid==0) {
			companyidmoren=Integer.parseInt(id);
		}
		if(companyid!=0) {
			companyidmoren=companyid;
		}
		
		
		if ("showdepart".equals(opt)) {
			
			out.print(JSON.toJSONString(depart.selectDepartment(0, null, companyid)));

		} else if ("showdepartlist".equals(opt)) {
			out.print(JSON.toJSONString(depart.selectDepartment(0, null, companyidmoren)));

		} else {
			Page<User> pageInfo = new Page<User>();
			int count =0;
			int DeptId =0;
			try {
				String pageno = request.getParameter("PageNo");
				if (pageno == null) {
					pageno = "1";
				}
				pageInfo.setCurrentPage(Integer.parseInt(pageno));
				pageInfo.setPageSize(6);
				int departid = (int) session.getAttribute("departid");
				String deptId = request.getParameter("departid");
				if (deptId == null) {
					deptId = "0";
				}
				if (departid != 0 && deptId == "0") {
					DeptId = departid;
					us.setDeptId(departid);
					userlist = user.selectAllUser(us, pageInfo.getCurrentPage(), pageInfo.getPageSize());
					 count = user.selectAllUserCount(departid, 0); // 根据部门id查到员工的数量
						
				} else if (!deptId.equals("0")) {
					DeptId = Integer.parseInt(deptId);
					us.setDeptId(Integer.parseInt(deptId));
					userlist = user.selectAllUser(us, pageInfo.getCurrentPage(), pageInfo.getPageSize());
					 count = user.selectAllUserCount(Integer.parseInt(deptId), 0); // 根据部门id查到员工的数量
						
				} else if (departid == 0 && deptId.equals("0")) {
					 DeptId = 0;
					 userlist = user.selectAllUser(null,pageInfo.getCurrentPage(), pageInfo.getPageSize());
					 count = user.selectAllUserCount(0, 0); // 根据部门id查到员工的数量
				}
					pageInfo.setTotalRow(count);
				    pageInfo.setPageList(userlist);
			} catch (Exception e) {

			}

			session.setAttribute("PageInfo", userlist);
			session.setAttribute("page", pageInfo);
			session.setAttribute("departid", DeptId);
            session.setAttribute("companyid", companyidmoren);
			
			//out.print(JSON.toJSONString(maplist));
			request.getRequestDispatcher("dept_userinfo.jsp").forward(request, response);

		}

	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
